<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>

<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.broadvision.data.common.PrimitiveType" %>
<%@ page import="com.broadvision.emarketing.criteriabuilder.DataTypeAggregator" %>

<c:set var="formBean" value="${emProfMvAggrConditionEditForm}" />
<c:set var="formBeanMap" value="${formBean.map}" />

<c:set var="viewBean" value="${emProfMvAggrConditionEditView}" />
<c:set var="viewBeanMap" value="${viewBean.map}" />

<!--start zone 5 content area here-->

<!--start page title area here-->
<SCRIPT language='Javascript'>
  function cancelAction(theForm){
    window.location = "<html-el:rewrite page="/em/cb/displayCriteriaBuilder.do"/>";
    return false;
  }

  <c:if test='${formBeanMap.actType == "create"}'>
    function goMvBranchConditionNewPage(theForm) {
      theForm.action = "<html-el:rewrite page="/em/cb/profMvBranchConditionEdit.do" />";
      theForm.submit();
      return false;
    }
  </c:if>

  function refreshPage(theForm) {
    theForm.action = "<html-el:rewrite page="/em/cb/profMvAggrConditionEdit.do" />";
    theForm.submit();
    return false;
  }

    function saveCondition(theForm) {
    if (!validateForm(theForm)) return false;
    theForm.submit();
    return false;
  }

  <%
    PrimitiveType attrType = (PrimitiveType) ((Map)pageContext.getAttribute("viewBeanMap")).get("aggrResultPrimitiveType");
    final String typeStr;
    if (attrType == PrimitiveType.DoubleType) typeStr = "double";
    else if (attrType == PrimitiveType.IntType) typeStr = "int";
    else if (attrType == PrimitiveType.LongType) typeStr = "long";
    else if (attrType == PrimitiveType.MoneyType) typeStr = "money";
    else typeStr = "string";
    pageContext.setAttribute("typeStr", typeStr);
  %>
  
  function validateForm(theForm) {
    var theValue = theForm.itemValue.value;
    var pass;
    <c:choose>
      <c:when test='${typeStr == "double"}'>
        pass = validateDouble(theValue);
      </c:when>
      <c:when test='${typeStr == "int"}'>
        pass = validateInt(theValue);
      </c:when>
      <c:when test='${typeStr == "long"}'>
        pass = validateLong(theValue);
      </c:when>
      <c:when test='${typeStr == "money"}'>
        pass = validateMoney(theValue);
      </c:when>
      <c:otherwise>
        theValue = theValue.replace(/^\s+/g, '').replace(/\s+$/g, '');
        theForm.itemValue.value = theValue;
        pass = validateString(theValue);
      </c:otherwise>
    </c:choose>
    if (!pass) theForm.itemValue.focus();
    return pass;
  }

    function validateString(theValue) {
      theValue = theValue.replace(/^\s+/g, '').replace(/\s+$/g, '');
      if (theValue.length == 0) {
        alert("<bean-el:message bundle="em" key="em.cb.alert.empty.field" />");
        return false;
      }
      return true;
    }

    function isAllDigits(argvalue) {
        argvalue = argvalue.toString();
        var validChars = "0123456789";
        var startFrom = 0;
        if (argvalue.substring(0, 2) == "0x") {
           validChars = "0123456789abcdefABCDEF";
           startFrom = 2;
        } else if (argvalue.charAt(0) == "0") {
           validChars = "01234567";
           startFrom = 1;
        } else if (argvalue.charAt(0) == "-") {
            startFrom = 1;
        }

        for (var n = startFrom; n < argvalue.length; n++) {
            if (validChars.indexOf(argvalue.substring(n, n+1)) == -1) return false;
        }
        return true;
    }
    
  function validateDouble(theValue) {
    var bValid = true;
    if (theValue.length == 0) {
      alert("<bean-el:message bundle="em" key="em.cb.alert.invalid.number" />");
      return false;
    }
    // remove '.' before checking digits
    var tempArray = value.split('.');
    //Strip off leading '0'
    var zeroIndex = 0;
    var joinedString= tempArray.join('');
    while (joinedString.charAt(zeroIndex) == '0') {
      zeroIndex++;
    }
    var noZeroString = joinedString.substring(zeroIndex,joinedString.length);
    if (!isAllDigits(noZeroString)) {
      alert("<bean-el:message bundle="em" key="em.cb.alert.invalid.number" />");
      bValid = false;
    } else {
       var iValue = parseFloat(value);
       if (isNaN(iValue)) {
         alert("<bean-el:message bundle="em" key="em.cb.alert.invalid.number" />");
         bValid = false;
       }
    }
    return bValid;
  }

  function validateInt(theValue) {
    var iValue = parseInt(theValue);
    if (isNaN(iValue) || iValue < -2147483648 || iValue > 2147483647) {
      alert("<bean-el:message bundle="em" key="em.cb.alert.invalid.int" />");
      return false;
    }
    return true;
  }

  function validateLong(theValue) {
    if (!isAllDigits(theValue)) {
      alert("<bean-el:message bundle="em" key="em.cb.alert.invalid.whole.number" />");
      return false;
    }
    return true;
  }

  function validateMoney(theValue) {
    return true;
  }
</SCRIPT>

<html-el:form action="/em/cb/profMvAggrConditionUpdate.do"> 
  <input type=hidden name="refresh" value="true">
  <html-el:hidden property="actType" />
  <html-el:hidden property="key" />
  <c:set var="pageTitle">
    <c:choose>
      <c:when test='${formBeanMap.actType == "create"}'>
        <bean-el:message bundle="em" key="em.cb.multivalue.aggr.create.pageTitle" />
      </c:when>
      <c:otherwise>
        <bean-el:message bundle="em" key="em.cb.multivalue.aggr.edit.pageTitle" />
      </c:otherwise>
    </c:choose>
  </c:set>

<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">
      <div class="pageTitle"><c:out value="${pageTitle}" /></div>
      <div class="errorMessage"><html-el:errors/></div>

      <!--end page title area here-->

      <div class="pageSubtitle">
        <span class="left"><c:out value="${pageTitle}" /></span>
        <span class="right">
          <c:if test='${formBeanMap.actType == "create"}'>
            <html-el:button property="mvBranchConditonNewBtn" styleClass="inputButton" onclick="javascript:return goMvBranchConditionNewPage(this.form)">
              <bean-el:message bundle="em" key="em.cb.multivalue.aggr.create.exist" />    
            </html-el:button>
          </c:if>
        </span>
      </div>
      <div class="pageInstructions">
        <bean-el:message bundle="em" key="em.cb.multivalue.aggr.pageInstruction" />
      </div>

      <!--start main content here-->
      <table class="labelForm" width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td class="labelField">
            <bean-el:size id="aggregatorListSize" name="viewBean" property="aggregatorList" />          
            <c:set var="aggregatorList">
               <c:choose>
                <c:when test="${aggregatorListSize == 1}">
                   <%
                     List aggrList = (List)((Map)pageContext.getAttribute("viewBeanMap")).get("aggregatorList");
                     DataTypeAggregator aggr = (DataTypeAggregator)aggrList.get(0);
                   %>
                   <input type="hidden" name="aggregatorId" value="<%=aggr.getAggregatorId()%>" >
                   <%=aggr.getFriendlyAggregatorName()%>
                </c:when>
                <c:otherwise>
                  <html-el:select property="aggregatorId" size="1" onchange="javascript:refreshPage(this.form)">
                    <html-el:optionsCollection name="viewBean" property="aggregatorList" label="friendlyAggregatorName" value="aggregatorId" /> 
                  </html-el:select>
                </c:otherwise>
              </c:choose>
            </c:set>
            <c:set var="itemValue">
              <html-el:text property="itemValue" size="30"/>
            </c:set>
            <c:set var="operatorList">
              <html-el:select property="operator" size="1">
                <html-el:optionsCollection name="viewBean" property="operatorList" label="friendlyName" value="name" /> 
              </html-el:select>
            </c:set>
            <bean-el:message bundle="em" key="em.cb.multivalue.aggr.whole.sentence" 
              arg0="${aggregatorList}" arg1="${viewBeanMap.friendlyAttributeName}" 
              arg2="${viewBeanMap.friendlyGroupName}" arg3="${operatorList}" arg4="${itemValue}" /> 
          </td>
        </tr>
      </table>

      <div class="pageButtonWrap" align="right">
        <input type="button" name="cancelBtn" value="<bean-el:message bundle="em" key="button.cancel"/>" class="inputButton" onclick="javascript:cancelAction(this.form)">
        <input type="submit" name="doneBtn" value="<bean-el:message bundle="em" key="button.done"/>" class="inputButton" onclick="javascript:return saveCondition(this.form);">
      </div>
    </td>
  </tr>
</table>
</html-el:form>
